<template>
  <div class="app-container">
    <!-- header -->
    <el-row class="header">
      <el-col :span="8">
        <el-input
          v-model="queryForm.query"
          placeholder="请输入角色名称"
          clearable
        />
        <el-button type="primary" :icon="Search" round @click="initDate"
          >搜索</el-button
        >
        <el-button
          type="success"
          :icon="Plus"
          round
          @click="handlerDialogValue(null)"
          >新增</el-button
        >
        <el-popconfirm
          title="你确定要删除所选的所有内容吗?"
          @confirm="handleDelete(null)"
        >
          <template #reference>
            <el-button
              type="danger"
              :disabled="deleteState"
              :icon="Delete"
              round
              >批量删除</el-button
            >
          </template>
        </el-popconfirm>
      </el-col>
    </el-row>

    <!-- main -->
    <el-table
      :data="tableData"
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="name" label="角色名" width="100" align="center" />
      <el-table-column
        prop="code"
        label="权限字符"
        width="200"
        align="center"
      />
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="200"
        align="center"
      />
      <el-table-column prop="remark" label="备注" />
      <el-table-column
        prop="action"
        label="操作"
        width="400"
        fixed="right"
        align="center"
      >
        <template v-slot="scope">
          <el-button
            type="primary"
            :icon="Tools"
            @click="handleMenuDialogValue(scope.row.id)"
            >分配权限</el-button
          >
          <el-button
            v-if="scope.row.code != 'admin'"
            type="primary"
            :icon="Edit"
            @click="handlerDialogValue(scope.row.id)"
          />
          <el-popconfirm
            v-if="scope.row.code != 'admin'"
            title="您确定要删除这条记录吗？"
            @confirm="handleDelete(scope.row.id)"
          >
            <template #reference>
              <el-button type="danger" :icon="Delete" />
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:currentPage="queryForm.pageNum"
      v-model:page-size="queryForm.pageSize"
      :page-sizes="[10, 20, 30, 40]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>

  <!-- 自定义弹出修改或新增Dialog -->
  <Dialog
    v-model="dialogFormVisible"
    :dialogFormVisible="dialogFormVisible"
    :id="id"
    :dialogTitle="dialogTitle"
    @initDate="initDate"
  />

  <MenuDialog
    v-model="menuDialogVisible"
    :menuDialogVisible="menuDialogVisible"
    :id="roleId"
    @initDate="initDate"
  />
</template>

<script setup>
import { reactive, ref } from "vue";
import requestUtil, { getServerUrl } from "@/util/request";
import {
  Search,
  Plus,
  Delete,
  DocumentAdd,
  Edit,
  Tools,
  RefreshRight,
} from "@element-plus/icons-vue";
import Dialog from "./components/dialog";
import { ElMessage, ElMessageBox } from "element-plus";
import MenuDialog from "./components/menuDialog";

let tableData = ref([]);
let total = ref(0);
let queryForm = ref({
  query: "",
  current: 1,
  pageSize: 10,
});

// 初始化数据
let initDate = async () => {
  const result = await requestUtil.post("role/list", queryForm.value);
  // console.log(result);
  tableData.value = result.data.roleList;
  total.value = result.data.total;
};
initDate();

//分页功能实现
//查询条数发生变化
let handleSizeChange = (pageSize) => {
  queryForm.value.current = 1;
  queryForm.value.pageSize = pageSize;
  initDate();
};

let handleCurrentChange = (currentPage) => {
  queryForm.value.current = currentPage;
  initDate();
};

//  Dialog add / update 功能
let dialogFormVisible = ref(false); //打开弹窗
let id = ref("");
let dialogTitle = ref("角色新增"); //弹窗标题

let handlerDialogValue = (roleId) => {
  // console.log(userId);
  if (roleId) {
    id.value = roleId;
    dialogTitle.value = "角色修改";
  } else {
    id.value = "";
    dialogTitle.value = "角色新增";
  }
  dialogFormVisible.value = true;
};

//删除用户操作
const deleteState = ref(true);
let multipleSelection = reactive({
  data: [],
});

//ids单个复选框的值加入
const handleSelectionChange = (val) => {
  console.log(val);
  multipleSelection.data = val; //此val 自动把所选复选框变为List集合
  deleteState.value = multipleSelection.data.length == 0;
};

//执行删除操作  判断是否为多人删除或者单人删除
const handleDelete = async (id) => {
  var ids = []; //单独一个删除 就会传入一个id  以此判断是否为单选 或 多选删除
  if (id) {
    ids.push(id);
  } else {
    multipleSelection.data.forEach((selectItem) => {
      console.log(selectItem.id);
      ids.push(selectItem.id);
    });
  }
  // console.log(ids);

  //开始删除
  const res = await requestUtil.post("role/delete", ids);
  if ((res.code = 20000)) {
    ElMessage({
      type: "success",
      message: res.message,
    });
    initDate();
  } else {
    ElMessage({
      type: "error",
      message: res.message,
    });
  }
};

//修改角色 开启子组件Dialog 操作TODO
const menuDialogVisible = ref(false);
const roleId = ref("");
const handleMenuDialogValue = (currentRoleId) => {
  console.log("roleId=" + roleId);
  roleId.value = currentRoleId;
  menuDialogVisible.value = true;
};
</script>

<style lang="scss" scoped>
.header {
  margin-bottom: 20px;
  .el-col {
    display: flex;
    .el-button {
      margin: 5px;
    }
  }
}
.app-container {
  padding: 0px;
  .el-pagination {
    display: flex;
    justify-content: flex-end;
  }
}

.el-pagination {
  float: right;
  padding: 20px;
  box-sizing: border-box;
}

::v-deep th.el-table__cell {
  word-break: break-word;
  background-color: #f8f8f9 !important;
  color: #515a6e;
  height: 40px;
  font-size: 13px;
}

.el-tag--small {
  margin-left: 5px;
}
</style>
